package com.jdls.tanhua.server;

import com.jdls.tanhua.dubbo.AnnouncementApi;
import com.jdls.tanhua.dubbo.CommentApi;
import com.jdls.tanhua.dubbo.FriendApi;
import com.jdls.tanhua.dubbo.UserInfoApi;
import com.jdls.tanhua.enums.CommentType;
import com.jdls.tanhua.exception.AllException;
import com.jdls.tanhua.interceptor.UserHolder;
import com.jdls.tanhua.mongo.Comment;
import com.jdls.tanhua.mongo.Friend;
import com.jdls.tanhua.pojo.Announcement;
import com.jdls.tanhua.pojo.UserInfo;
import com.jdls.tanhua.template.HuanXinTemplate;
import com.jdls.tanhua.vo.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


/**
 * @author 嘉德罗斯
 * @date 2021/10/30
 */

@Service
public class MessagesServer {

    @DubboReference
    private AnnouncementApi announcementApi;

    @DubboReference
    private CommentApi commentApi;


    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private FriendApi friendApi;

    //根据环信用户id查询用户详情
    public UserInfoVo userInfo(String huanxinId) {
        if(huanxinId==null){
            return null;
        }
        Long id = Long.valueOf(huanxinId.substring(2));
        UserInfo userInfo = userInfoApi.find(id);
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,vo);

        if(userInfo.getAge()!=null){
            vo.setAge(userInfo.getAge());
        }
        return vo;
    }

    //添加好友
    public void contacts(Long userId) {

        Long id = UserHolder.getUserId();
        //双向添加好友
        boolean flag = friendApi.save(id,userId);
        if (!flag){
            throw new AllException(ErrorResult.friendErr());
        }
        //环信添加好友
        huanXinTemplate.addContact("hx"+id,"hx"+userId);
    }

    //查询好友列表
    public PageResult findAll(Integer page, Integer pagesize, String keyword) {

        Long userId = UserHolder.getUserId();

        List<Friend> friends = friendApi.findAll(userId,page,pagesize);

        List<Long> ids = new ArrayList<>();
        for (Friend friend : friends) {
            ids.add(friend.getFriendId());
        }

        List<UserInfo> userInfos = userInfoApi.findAll(ids,keyword);
        List<ContactVo> contactVos= new ArrayList<>();
        for (UserInfo userInfo : userInfos) {
            ContactVo contactVo = new ContactVo();
            contactVo.setAge(Integer.valueOf(userInfo.getAge()));
            contactVo.setCity(userInfo.getCity());
            contactVo.setAvatar(userInfo.getAvatar());
            contactVo.setGender(userInfo.getGender());
            contactVo.setNickname(userInfo.getNickname());
            contactVo.setUserId(String.valueOf(userInfo.getId()));
            contactVos.add(contactVo);
        }

        PageResult pageResult = new PageResult(page, pagesize, contactVos.size(), contactVos);

        return pageResult;
    }

    //查询点赞,评论或喜欢列表
    public PageResult type(Integer page, Integer pagesize,String type) {
        Long id = UserHolder.getUserId();
        System.out.println();
        List<Comment> comments;
        //判断路径是否为点赞
        if ("likes".equals(type)){
            //根据当前用户id获取点赞过当前用户的信息
            comments= commentApi.findLike(id,page,pagesize,CommentType.LIKE.getType());
        }else if("loves".equals(type)){
            //根据当前用户id获取喜欢过当前用户的信息
            comments= commentApi.findLike(id,page,pagesize,CommentType.LOVE.getType());
        }else {
            //根据当前用户id获取评论过当前用户的信息
            comments= commentApi.findLike(id,page,pagesize,CommentType.COMMENT.getType());

        }
        //获取点赞,评论或者喜欢过当前用户的id集合
        List<Long> ids = new ArrayList<>();
        for (Comment comment : comments) {
            ids.add(comment.getPublishUserId());
        }
        //获取点赞，评论或喜欢用户集合的用户基本数据
        List<UserInfo> userInfos = userInfoApi.findAll(ids);
        //构造返回值
        List<LikeVo> items = new ArrayList<>();
        LikeVo likeVo = new LikeVo();
        for (Comment comment : comments) {
            for (UserInfo userInfo : userInfos) {
                if (comment.getPublishUserId() == userInfo.getId()){
                    likeVo.setAvatar(userInfo.getAvatar());
                    String format = new SimpleDateFormat("yyyy-MM-dd").format(comment.getCreated());
                    likeVo.setCreateDate(format);
                    likeVo.setId(userInfo.getId().toString());
                    likeVo.setNickname(userInfo.getNickname());
                    items.add(likeVo);
                    //userInfos.remove(userInfo);
                    break;
                }
            }
        }

        PageResult pageResult = new PageResult(page,pagesize,items.size(),items);
        return pageResult;
    }

    //查询公告列表
    public PageResult announcements(Integer page, Integer pagesize) {
        List<Announcement> announcements = announcementApi.findAll();
        List<AnnouncementVo> items = new ArrayList<>();
        AnnouncementVo announcementVo = new AnnouncementVo();
        for (Announcement announcement : announcements) {
            announcementVo.setId(announcement.getId().toString());
            String format = new SimpleDateFormat("yyyy-MM-dd").format(announcement.getCreated());
            announcementVo.setCreateDate(format);
            announcementVo.setTitle(announcement.getTitle());
            announcementVo.setDescription(announcement.getDescription());
            items.add(announcementVo);
        }
        PageResult pageResult = new PageResult(page,pagesize,items.size(),items);
        return pageResult;
    }
}
